package cn.zahuo.SJR.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
@SuppressWarnings("ALL")
public class JDBC_Update_noThrows {

    public static void main(String[] args) {
        Connection connection=null;
        Statement Stmt=null;
        try {
            //        1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.定义sql
            String sql="insert into emp values(null,'燕赤霞','男',8888.00,'2000-12-1',1)";
//            修改已有的记录
//            String sql="update  emp set salary=6666.00 where  name ='燕赤霞'";
//          删除已有的记录
//            String sql="delete from emp where name ='燕赤霞'";
//             3。获取Connection对象
            connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/moretables?&useSSL=false&serverTimezone=UTC","root","root");
//        4.获取执行sql的对象 Statement
            Stmt=connection.createStatement();
//            5.执行sql，并且记录return，注意库里创建表用update也可以 
            int count=Stmt.executeUpdate(sql);
//            根据返回值，输出提示信息
            if (count > 0) {
                System.out.println("数据添加成功！");
                System.out.println("本次操作影响了"+count+"条记录！");
            }
            else {
                System.out.println("数据添加失败~~~");
                System.out.println("本次操作影响了"+count+"条记录！");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
//             抓取到空指针异常
            if (Stmt!=null)
            {
                try {
                    Stmt.close();
                } catch (SQLException throwables) {
//                    处理初始时的null
                    throwables.printStackTrace();
                }
            }
            if(connection !=null){
                try {

                    connection.close();
                } catch (SQLException throwables) {
//                    处理初始时的null
                    throwables.printStackTrace();
                }
            }
        }
    }

}
